﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Rigsby.EventBasedWcfAsyncOperation
{
	public partial class Form1 : Form
	{
		ServiceReference.MyServiceClient m_Client;
		string m_MyOperationId;

		public Form1()
		{
			InitializeComponent();

			m_Client = new ServiceReference.MyServiceClient();
			m_Client.GetSquareRootCompleted +=
				new EventHandler<ServiceReference.GetSquareRootCompletedEventArgs>(client_GetSquareRootCompleted);
		}

		private void m_StartButton_Click(object sender, EventArgs e)
		{
			double value = 0;
			Double.TryParse(m_ValueTextBox.Text, out value);

			m_MyOperationId = Guid.NewGuid().ToString();

			m_Client.GetSquareRootAsync(value, m_MyOperationId);

			m_StartButton.Enabled = false;
			m_ResultTextBox.Text = "Loading...";
		}

		void client_GetSquareRootCompleted(
			object sender, 
			Rigsby.EventBasedWcfAsyncOperation.ServiceReference.GetSquareRootCompletedEventArgs e)
		{
			if ((string)e.UserState == m_MyOperationId)
			{
				m_ResultTextBox.Text = e.Result.ToString();

				m_StartButton.Enabled = true;
			}
		}

        private void Form1_Load(object sender, EventArgs e)
        {

        }
	}
}
